package com.example.administrator.runbook;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.example.administrator.runbook.findfragment.Utils;
import com.example.administrator.runbook.fragments.CommonFragment;
import com.example.administrator.runbook.fragments.FindFragment;
import com.example.administrator.runbook.fragments.IndexFragment;
import com.example.administrator.runbook.fragments.adapter.FragmentAdapter;
import com.example.administrator.runbook.fragments.community.CommunityFragment;
import com.flyco.tablayout.SlidingTabLayout;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<CommonFragment> mFragmentList;
    private FragmentAdapter mFragmentAdapter;
    private Toolbar mToolbar;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Utils.verifyStoragePermissions(this);
        initView();

        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        mToolbar.inflateMenu(R.menu.main_menu);

        SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.tab_layout_main);
        viewPager = (ViewPager) findViewById(R.id.view_pager_main);
        mFragmentList = new ArrayList<>();
        mFragmentList.add(new IndexFragment());
        mFragmentList.add(new CommunityFragment());
        mFragmentList.add(new FindFragment());
        mFragmentAdapter = new FragmentAdapter(
                getSupportFragmentManager(),
                mFragmentList
        );
        viewPager.setAdapter(mFragmentAdapter);
        viewPager.setOffscreenPageLimit(2);
        slidingTabLayout.setViewPager(viewPager);
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

    public void skip() {
        viewPager.setCurrentItem(2, true);
    }
}
